package com.sgcc.ln.pmos.common.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.core.content.ContextCompat;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.sgcc.pmos.nativesdk.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChaoAJ
 * @describe 2条线的折线图
 * @time 2022/8/29 14:10
 */
public class TwoLineChartView extends LinearLayout {

    private LineChart lcLineChart;
    private List<String> yAxis;
    private List<String> yAxis2;
    private String[] xAxisValue = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9",
            "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};


    public TwoLineChartView(Context context, List<String> yAxis, List<String> yAxis2) {
        super(context);
        this.yAxis = yAxis;
        this.yAxis2 = yAxis2;
        initLineChart();
    }


    private void initLineChart() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_line_chart_layout, this);
        lcLineChart = view.findViewById(R.id.lcLineChart);
        lcLineChart.getDescription().setEnabled(false);
        lcLineChart.setTouchEnabled(true);
        lcLineChart.setDragEnabled(true);
        lcLineChart.setPinchZoom(false);
        lcLineChart.setScaleEnabled(false);
        lcLineChart.setDrawGridBackground(false);
        lcLineChart.setMaxHighlightDistance(300);
        List<List<String>> list = new ArrayList<>();
        list.add(yAxis);
        list.add(yAxis2);
        TwoLineMarkerView mv = new TwoLineMarkerView(getContext(), xAxisValue, list);
        mv.setChartView(lcLineChart);
        lcLineChart.setMarker(mv);

        XAxis x = lcLineChart.getXAxis();
        x.setTextColor(Color.parseColor("#999999"));
        x.setDrawGridLines(false);
        x.setDrawAxisLine(true);
        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setAxisLineWidth(1);
        x.setLabelCount(12);
        x.setAxisLineColor(Color.parseColor("#DCDCDC"));
        x.setValueFormatter(new IndexAxisValueFormatter(xAxisValue));

        YAxis y = lcLineChart.getAxisLeft();
        y.setTextColor(Color.parseColor("#999999"));
        y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        y.setDrawAxisLine(true);
        y.setGranularity(0.02f);


        lcLineChart.getAxisRight().setEnabled(false);

        Legend l = lcLineChart.getLegend();
        l.setEnabled(false);

        setData();
    }

    private void setData() {
        if (null == yAxis)
            yAxis = new ArrayList<>();

        ArrayList<Entry> values = new ArrayList<>();
        if (null != yAxis && yAxis.size() > 0)
            for (int i = 0; i < yAxis.size(); i++) {
                float val = Float.parseFloat(yAxis.get(i));
                values.add(new Entry(i, val));
            }

        LineDataSet set1 = new LineDataSet(values, "");
        set1.setDrawValues(false);
        set1.setLineWidth(1.5f);
        set1.setCircleRadius(3f);
        set1.setCircleColor(Color.parseColor("#0092FF"));
        set1.setColor(Color.parseColor("#0092FF"));
        set1.setCircleHoleRadius(2f);
        set1.setDrawFilled(true);
        Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.line_chart_fill);
        set1.setFillDrawable(drawable);
        set1.setFillFormatter((dataSet, dataProvider) ->
                lcLineChart.getAxisLeft().getAxisMinimum());




        ArrayList<Entry>  values2= new ArrayList<>();
        if (null != yAxis2 && yAxis2.size() > 0)
            for (int i = 0; i < yAxis2.size(); i++) {
                float val = Float.parseFloat(yAxis2.get(i));
                values2.add(new Entry(i, val));
            }

        LineDataSet set2 = new LineDataSet(values2, "");
        set2.setDrawValues(false);
        set2.setLineWidth(1.5f);
        set2.setCircleRadius(3f);
        set2.setCircleColor(Color.parseColor("#FFA500"));
        set2.setColor(Color.parseColor("#FFA500"));
        set2.setCircleHoleRadius(2f);
        set2.setDrawFilled(true);
        set2.setFillDrawable(drawable);
        set2.setFillFormatter((dataSet, dataProvider) ->
                lcLineChart.getAxisLeft().getAxisMinimum());

        lcLineChart.setData(new LineData(set1,set2));

    }


}